package fu.ck.Util;

import com.google.gson.Gson;
import fu.ck.Model.HongbaoModel;
import fu.ck.Model.SettingModel;
import fu.ck.ResultModel.ResultModel;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Hashtable;

/**
 * Created by xuxingcan on 16-11-5.
 */
public class CalculationUtil {
    private static Hashtable<String, ArrayList<String>> sendUserId2sendId = new Hashtable<>();
    private static Hashtable<String, Integer> sendId2Money = new Hashtable<>();
    private static Hashtable<String, String> sendId2sendUserId = new Hashtable<>();
    private static Gson gson = new Gson();

    public static ResultModel calculate(SettingModel settingModel, String json) {
        ResultModel resultModel = new ResultModel();
        int setMoney = (int) (settingModel.setMoney * 100);
        if (setMoney <= 0) {
            resultModel.errorMsg = "请先设置money!";
            return resultModel;
        }
        HongbaoModel hongbaoModel = gson.fromJson(json, HongbaoModel.class);
        String sendUserId = sendId2sendUserId.get(hongbaoModel.sendId);
        if (sendUserId == null) {
            resultModel.errorMsg = "未找到sendUserId,请联系管理员!";
            return resultModel;
        }
        resultModel.sendUserId = sendUserId;
        ArrayList<String> sendIdList = sendUserId2sendId.get(sendUserId);
        if (sendIdList == null) {
            resultModel.errorMsg = "未找到sendIdList,请联系管理员!";
            return resultModel;
        }
        int n = 0;
        sendId2Money.put(hongbaoModel.sendId, hongbaoModel.amount);
        for (String sendId : sendIdList) {
            Integer money = sendId2Money.get(sendId);
            if (money == null) {
                resultModel.errorMsg = "未找到sendId,请联系管理员!";
                return resultModel;
            }
            n += money;
        }
        resultModel.isEnough = n >= setMoney;
        if (resultModel.isEnough) {
            clean(sendUserId);
        }
        return resultModel;
    }

    private static void clean(String sendUserId) {
        ArrayList<String> sendIdList = sendUserId2sendId.remove(sendUserId);
        for (String sendId : sendIdList) {
            sendId2sendUserId.remove(sendId);
            sendId2Money.remove(sendId);
        }
    }

    public static void main(String[] args) throws Exception {
        getNativeUrl("", "<msg><appmsg appid=\"\" sdkver=\"\"><des><![CDATA[我给你发了一个红包，赶紧去拆!]]></des><url><![CDATA[https://wxapp.tenpay.com/mmpayhb/wxhb_personalreceive?showwxpaytitle=1&msgtype=1&channelid=1&sendid=1000039401201702197044684855267&ver=6&sign=1b01dd288742691854dfd808755a1be31d6d6b86a7a8afc0793d459b1f267de57b1edcd54660e886e38f5ab372a55d06b7a7b22c1d53e2b88dd3385c783380dd8948c784624801354d6b9320bd93abda249626a5dfe705793f8c01ad2df174f3]]></url><type><![CDATA[2001]]></type><title><![CDATA[微信红包]]></title><thumburl><![CDATA[http://wx.gtimg.com/hongbao/1701/hb.png]]></thumburl><wcpayinfo><templateid><![CDATA[7a2a165d31da7fce6dd77e05c300028a]]></templateid><url><![CDATA[https://wxapp.tenpay.com/mmpayhb/wxhb_personalreceive?showwxpaytitle=1&msgtype=1&channelid=1&sendid=1000039401201702197044684855267&ver=6&sign=1b01dd288742691854dfd808755a1be31d6d6b86a7a8afc0793d459b1f267de57b1edcd54660e886e38f5ab372a55d06b7a7b22c1d53e2b88dd3385c783380dd8948c784624801354d6b9320bd93abda249626a5dfe705793f8c01ad2df174f3]]></url><iconurl><![CDATA[http://wx.gtimg.com/hongbao/1701/hb.png]]></iconurl><receivertitle><![CDATA[恭喜发财，大吉大利]]></receivertitle><sendertitle><![CDATA[恭喜发财，大吉大利]]></sendertitle><scenetext><![CDATA[微信红包]]></scenetext><senderdes><![CDATA[查看红包]]></senderdes><receiverdes><![CDATA[领取红包]]></receiverdes><nativeurl><![CDATA[wxpay://c2cbizmessagehandler/hongbao/receivehongbao?msgtype=1&channelid=1&sendid=1000039401201702197044684855267&sendusername=xuxingcan&ver=6&sign=1b01dd288742691854dfd808755a1be31d6d6b86a7a8afc0793d459b1f267de57b1edcd54660e886e38f5ab372a55d06b7a7b22c1d53e2b88dd3385c783380dd8948c784624801354d6b9320bd93abda249626a5dfe705793f8c01ad2df174f3]]></nativeurl><sceneid><![CDATA[1002]]></sceneid><innertype><![CDATA[0]]></innertype><scenetext>微信红包</scenetext></wcpayinfo></appmsg><fromusername><![CDATA[xuxingcan]]></fromusername></msg>\n" +
                "         ");
    }

    private static final String NativeUrlStart = "<nativeurl><![CDATA[";
    private static final String NativeUrlEnd = "]]></nativeurl>";

    public static ResultModel getNativeUrl(String sendUserId, String msg) {
        /**
         <msg><appmsg appid="" sdkver=""><des><![CDATA[我给你发了一个红包，赶紧去拆!]]></des><url><![CDATA[https://wxapp.tenpay.com/mmpayhb/wxhb_personalreceive?showwxpaytitle=1&msgtype=1&channelid=1&sendid=1000039401201702197044684855267&ver=6&sign=1b01dd288742691854dfd808755a1be31d6d6b86a7a8afc0793d459b1f267de57b1edcd54660e886e38f5ab372a55d06b7a7b22c1d53e2b88dd3385c783380dd8948c784624801354d6b9320bd93abda249626a5dfe705793f8c01ad2df174f3]]></url><type><![CDATA[2001]]></type><title><![CDATA[微信红包]]></title><thumburl><![CDATA[http://wx.gtimg.com/hongbao/1701/hb.png]]></thumburl><wcpayinfo><templateid><![CDATA[7a2a165d31da7fce6dd77e05c300028a]]></templateid><url><![CDATA[https://wxapp.tenpay.com/mmpayhb/wxhb_personalreceive?showwxpaytitle=1&msgtype=1&channelid=1&sendid=1000039401201702197044684855267&ver=6&sign=1b01dd288742691854dfd808755a1be31d6d6b86a7a8afc0793d459b1f267de57b1edcd54660e886e38f5ab372a55d06b7a7b22c1d53e2b88dd3385c783380dd8948c784624801354d6b9320bd93abda249626a5dfe705793f8c01ad2df174f3]]></url><iconurl><![CDATA[http://wx.gtimg.com/hongbao/1701/hb.png]]></iconurl><receivertitle><![CDATA[恭喜发财，大吉大利]]></receivertitle><sendertitle><![CDATA[恭喜发财，大吉大利]]></sendertitle><scenetext><![CDATA[微信红包]]></scenetext><senderdes><![CDATA[查看红包]]></senderdes><receiverdes><![CDATA[领取红包]]></receiverdes><nativeurl><![CDATA[wxpay://c2cbizmessagehandler/hongbao/receivehongbao?msgtype=1&channelid=1&sendid=1000039401201702197044684855267&sendusername=xuxingcan&ver=6&sign=1b01dd288742691854dfd808755a1be31d6d6b86a7a8afc0793d459b1f267de57b1edcd54660e886e38f5ab372a55d06b7a7b22c1d53e2b88dd3385c783380dd8948c784624801354d6b9320bd93abda249626a5dfe705793f8c01ad2df174f3]]></nativeurl><sceneid><![CDATA[1002]]></sceneid><innertype><![CDATA[0]]></innertype><scenetext>微信红包</scenetext></wcpayinfo></appmsg><fromusername><![CDATA[xuxingcan]]></fromusername></msg>
         */
        try {
            msg = URLDecoder.decode(msg, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        ResultModel resultModel = new ResultModel();
        int start = msg.indexOf(NativeUrlStart) + NativeUrlStart.length();
        int end = msg.indexOf(NativeUrlEnd);
        if (start != -1 && end != -1) {
            resultModel.nativeUrl = msg.substring(start, end).trim();
            resultModel.sendUserId = sendUserId;
            String[] strings = resultModel.nativeUrl.split("&");
            for (String s : strings) {
                if (s != null && s.startsWith("sendid=")) {
                    ArrayList<String> list = sendUserId2sendId.get(sendUserId);
                    if (list == null) {
                        list = new ArrayList<>();
                        sendUserId2sendId.put(sendUserId, list);
                    }
                    String sendid = s.replace("sendid=", "");
                    list.add(sendid);
                    sendId2sendUserId.put(sendid, sendUserId);
                    break;
                }
            }
        } else {
            resultModel.errorMsg = "未找到url,请联系管理员!";
        }
        return resultModel;
    }
}